﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (IsPostBack)
        
       { 
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
        con.Open();
        string cmdStr = "Select count(*)from Registration where userName='" + TextBoxUN.Text + "'";
        SqlCommand userExist = new SqlCommand(cmdStr, con);
        int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
        con.Close();
        if (temp == 1)
        {
            Response.Write("user Name Already Exist....!!!<br /> Please Choose Another user Name.  ");

        }
    }
}

      
protected void  submit_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
    con.Open();
    string insCmd = "Insert into Registration(UserName, Password, EmailAddress, FullName,Country) values (@UserName,@Password,@EmailAddress,@FullName,@Country)";
    SqlCommand insertUser = new SqlCommand(insCmd, con);
    insertUser.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
    insertUser.Parameters.AddWithValue("@Password", TextBoxPass.Text);
    insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
    insertUser.Parameters.AddWithValue("@FullName", TextBoxFN.Text);
    insertUser.Parameters.AddWithValue("@Country", DropDownListCountry.SelectedItem.ToString());


    try
    {
        insertUser.ExecuteNonQuery();
        con.Close();
        Response.Redirect("Login.aspx");


    }

    catch (Exception ex)
    {
        if (ex is SqlException)
        {
            Response.Write("<b>Something Really Bad Happened...Please try Again.</b>");
        }
    }
    finally
    {
        //Any Special Action you want to Add.........
    }

}

}